package com.xiangxiao.rpan.storage.controller;

import com.sun.org.apache.bcel.internal.generic.FSTORE;
import com.xiangxiao.rpan.storage.constant.ResponseCode;
import com.xiangxiao.rpan.storage.constant.ResponseMessage;
import com.xiangxiao.rpan.storage.dto.JsonObject;
import com.xiangxiao.rpan.storage.entity.FilterConfig;
import com.xiangxiao.rpan.storage.service.FilterConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/11/16 20:24
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class FilterConfigController {
  private static Logger logger = LoggerFactory.getLogger(FilterConfigController.class);
  @Resource
  private FilterConfigService filterConfigService;

  @GetMapping("/storage/{storageId}/filters")
  public JsonObject getFilters(@PathVariable Integer storageId) {
    try {
      List<FilterConfig> configs = filterConfigService.findByStorageId(storageId);
      return new JsonObject(ResponseMessage.QUERY_STORAGE_FILTRER_LIST_SUCCESS, ResponseCode.OK, configs);
    } catch(Exception e) {
      logger.error(ResponseMessage.QUERY_STORAGE_FILTRER_LIST_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_STORAGE_FILTRER_LIST_FAILED, ResponseCode.QUERY_STORAGE_FILTRER_LIST_FAILED);
    }
  }

  @PostMapping("/storage/{storageId}/filters")
  public JsonObject saveFilters(@PathVariable Integer storageId, @RequestBody List<FilterConfig> filters){
    try {
      filterConfigService.batchSave(storageId, filters);
      return new JsonObject(ResponseMessage.SAVE_FILTRER_RULES__SUCCESS, ResponseCode.OK, true);
    } catch (Exception e) {
      logger.error(ResponseMessage.SAVE_FILTRER_RULES__FAILED, e);
      return new JsonObject(ResponseMessage.SAVE_FILTRER_RULES__FAILED, ResponseCode.SAVE_FILTRER_RULES__FAILED);
    }
  }
}
